package com.tengfei.api.user;

import com.tengfei.common.annotation.Log;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.main.system.domain.SysUser;
import com.tengfei.main.system.service.ISysUserService;
import com.tengfei.main.user.domain.bo.info.UserInfoBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author feige
 * @version v1.0
 * @date 2022-08-14-16:32
 * @description 用户信息门户
 */

@RestController
@RequestMapping("/user/info")
@Validated
@Api(value = "用户信息控制器", tags = {"用户个人信息模块"})
public class UserInfoController extends BaseController {

    @Resource
    private ISysUserService userService;

    /**
     * 用户个人信息
     */
    @ApiOperation("用户个人信息")
    @GetMapping("profile")
    public R<Map<String, Object>> getUserInfo() {
        SysUser user = userService.selectUserById(getUserId());
        Map<String, Object> ajax = new HashMap<>();
        ajax.put("user", user);
        ajax.put("roleGroup", userService.selectUserRoleGroup(user.getUserName()));
        ajax.put("postGroup", userService.selectUserPostGroup(user.getUserName()));
        return R.ok(ajax);
    }

    /**
     * 重置密码
     */
    @ApiOperation("重置密码")
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePwd")
    public R<Void> updatePwd(String oldPassword, String newPassword) {
        return userService.resetUserPwd(oldPassword, newPassword);
    }


    /**
     * 修改用户个人信息
     */
    @ApiOperation("修改用户个人信息")
    @Log(title = "修改用户个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> updateProfile(@RequestBody UserInfoBo user) {
        return userService.updateUserInfo(user);
    }

    /**
     * 用户头像上传
     */
    @ApiOperation("用户头像上传")
    @Log(title = "用户头像上传", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public R<String> avatar(@RequestPart("avatar") MultipartFile file) {
        return R.ok("上传成功", userService.uploadAvatar(file));
    }

}
